﻿function SelectProduct(obj, url) {

    if ($(obj).combo("options").disabled == true) {
        return;
    }

    // 验证数据
    var dependItemValue;
    if ($(obj).combobox("options").dependItemID > 0) {
        $(obj).combo('textbox').validatebox('validate', true);
        dependItemValue = obj.exDependItemValue;

        // 当依赖栏位为空时，中断弹出动作
        if (!amCommon.notEmpty(dependItemValue)) {
            obj.exDependItemValue = undefined;
            return;
        }
    }
    obj.exDependItemValue = undefined;

    var value = "";
    var valueObj = [];
    var usageType = 'SingleSelection';
    var options = $(obj).combobox("options");
    if (options.multiple == true) {
        usageType = 'MultiSelection';
        valueObj = $(obj).combobox('getValues');
        value = valueObj;
        if (value.join) {
            value = value.join(',');
        }
    }
    else {
        if ($(obj).combo('options').exUsageListFilter == true) {
            usageType = 'SingleListFilter';
        }

        valueObj = $(obj).combobox('getValue');
        value = valueObj;
    }

    // 记录下旧的值对象，用于撤销操作时恢复值
    obj.exOldValue = valueObj;

    // 弹出框时，隐藏Combo下拉框
    $(obj).combo('hidePanel');
    foreignTableID = 0;
    var newLayer = window.showBusinessDialog(url, "商品信息", null, null, null, 'SingleReferCopy');//=  gridListHandler.showReferDialog('/admin/LBUser/index',"双击选择");
    //if (options.foreignViewID > 0) {
    //    newLayer = window.showSummaryDialog(options.foreignViewID, foreignTableDisplayName,
    //        value, options.dependDecideSummaryItemID, dependItemValue, usageType, me.iExtraFilter);
    //}
    //else if (foreignTableID > 0) {
    //    newLayer = window.showBusinessDialog(foreignTableID, foreignTableDisplayName,
    //        value, options.dependDecideItemID, dependItemValue, usageType, me.iExtraFilter);
    //}
    //else if (options.foreignTableUrl) {
    //    newLayer = window.showBusinessDialog(options.foreignTableUrl, foreignTableDisplayName,
    //        value, options.dependDecideItemID, dependItemValue, usageType, me.iExtraFilter);
    //}

    // 当弹出框点击取消按钮时，撤销对本栏位的修改
    newLayer.rejectSelectValue = function () {
        var oldValues = obj.exOldValue;
        if (oldValues == null || oldValues == undefined) {
            oldValues = new Array();
        }
        me.setComboValue(oldValues);

        top.closeDialog();
    }

    // 当弹出框选择了业务数据后，更新父页面的文本框
    newLayer.updatePopupSelectValue = function (selectedRows, isUnSelect, keepPopupWindow) {

        if (selectedRows && selectedRows.length > 0) {

            var selections = new Array();
            var selectedValues = new Array();
            for (var i = selectedRows.length - 1; i >= 0; i--) {

                var selectRow = selectedRows[i];

                if (selectRow) {
                    var selectValue = selectRow._ID;
                    if (selectValue == undefined) {
                        selectValue = selectRow.id;
                    }
                    var selectText = selectRow["Productname"];
                    if (selectText == null) {
                        selectText = selectRow['SystemName'];
                    }

                    selectedValues.push(selectValue);
                    selections.push({ value: selectValue, text: selectText });
                }
            }

            var options = $(obj).combobox("options");
            var oldValues = $(obj).combobox('getValues');

            if (!options.data || !options.data.mergeSelections) {
                options.data = new Array();
            }
            options.data = options.data.mergeSelections(selections);
            $(obj).combobox("loadData", options.data);

            if (!isUnSelect) {

                $(obj).combobox("setText", selections[0].text);
                $(obj).combobox("setValue", selections[0].value);
            }
        }


        top.closeDialog();

    };

}